CUDA enabled hardware and .NET 4 (Visual Studio 2010 IDE or C# Express 2010) is needed to successfully run the example code. Visual C++ Express 2008 has been used as a CUDA C editor (2010 version has changed custom build rules feature and cannot work with that provided by CUDA SDK for easy VS integration).
Showing posts with label C Sharp. Show all posts
Showing posts with label C Sharp. Show all posts
What is Foreground or Background Thread
Threading is a concept we all understand and most of us would have implemented them in real world applications. Here is one more explanation of inner level detail on threads - that is Foreground and Background threads.
Converting Numbers to Words in C#
The program supports both the US and UK numbering systems. For example the number 620 would be expressed as follows:
Six Hundred Twenty (in the US system)
Six Hundred and Twenty (in the UK system)
Implementing the QT Algorithm using C#
Quality Threshold (or QT) is an algorithm used in cluster analysis which is described in this Wikipedia article (http://en.wikipedia.org/wiki/Cluster_analysis).
The basic idea of cluster analysis is to partition a set of points into clusters which have some relationship to each other. In the case of QT the user chooses the maximum diameter (i.e. distance between points) that a cluster can have and each point is then considered in turn, along with its neighbors, and allocated to a cluster.
The source code is listed below in case it is of interest to anyone else involved in this specialized field.
The basic idea of cluster analysis is to partition a set of points into clusters which have some relationship to each other. In the case of QT the user chooses the maximum diameter (i.e. distance between points) that a cluster can have and each point is then considered in turn, along with its neighbors, and allocated to a cluster.
The source code is listed below in case it is of interest to anyone else involved in this specialized field.
Source code including sample data
using System;using System.Collections.Generic;
using System;using System.Collections.Generic;
struct Point{
public int X, Y;
public int X, Y;
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
{
this.X = x;
this.Y = y;
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
public static int DistanceSquared(Point p1, Point p2)
{
int diffX = p2.X - p1.X;
int diffY = p2.Y - p1.Y;
return diffX * diffX + diffY * diffY;
}
}
{
return String.Format("({0}, {1})", X, Y);
}
public static int DistanceSquared(Point p1, Point p2)
{
int diffX = p2.X - p1.X;
int diffY = p2.Y - p1.Y;
return diffX * diffX + diffY * diffY;
}
}
static class QT{
static void Main()
{
List<Point> points = new List<Point>();
static void Main()
{
List<Point> points = new List<Point>();
// sample data points.Add(new Point(0,100));
points.Add(new Point(0,200));
points.Add(new Point(0,275));
points.Add(new Point(100, 150));
points.Add(new Point(200,100));
points.Add(new Point(250,200));
points.Add(new Point(0,200));
points.Add(new Point(0,275));
points.Add(new Point(100, 150));
points.Add(new Point(200,100));
points.Add(new Point(250,200));
double maxDiameter = 150.0;
List<List<Point>> clusters = GetClusters(points, maxDiameter);
Console.Clear();
Console.Clear();
// print points to console Console.WriteLine("The {0} points are :\n", points.Count);
foreach(Point p in points) Console.Write(" {0} ", p);
Console.WriteLine();
foreach(Point p in points) Console.Write(" {0} ", p);
Console.WriteLine();
// print clusters to console for(int i = 0; i < clusters.Count; i++)
{
int count = clusters[i].Count;
string plural = (count != 1) ? "s" : "";
Console.WriteLine("\nCluster {0} consists of the following {1} point{2} :\n", i + 1, count, plural);
foreach(Point p in clusters[i]) Console.Write(" {0} ", p);
Console.WriteLine();
}
{
int count = clusters[i].Count;
string plural = (count != 1) ? "s" : "";
Console.WriteLine("\nCluster {0} consists of the following {1} point{2} :\n", i + 1, count, plural);
foreach(Point p in clusters[i]) Console.Write(" {0} ", p);
Console.WriteLine();
}
Console.ReadKey();
}
}
static List<List<Point>> GetClusters(List<Point> points, double maxDiameter)
{
if (points == null) return null;
points = new List<Point>(points); // leave original List unaltered List<List<Point>> clusters = new List<List<Point>>();
{
if (points == null) return null;
points = new List<Point>(points); // leave original List unaltered List<List<Point>> clusters = new List<List<Point>>();
while (points.Count > 0)
{
List<Point> bestCandidate = GetBestCandidate(points, maxDiameter);
clusters.Add(bestCandidate);
}
return clusters;
}
{
List<Point> bestCandidate = GetBestCandidate(points, maxDiameter);
clusters.Add(bestCandidate);
}
return clusters;
}
/* GetBestCandidate() returns first candidate cluster encountered if there is more than one
with the maximum number of points.
*/ static List<Point> GetBestCandidate(List<Point> points, double maxDiameter)
{
double maxDiameterSquared = maxDiameter * maxDiameter; // square maximum diameter List<List<Point>> candidates = new List<List<Point>>(); // stores all candidate clusters List<Point> currentCandidate = null; // stores current candidate cluster int[] candidateNumbers = new int[points.Count]; // keeps track of candidate numbers to which points have been allocated int totalPointsAllocated = 0; // total number of points already allocated to candidates int currentCandidateNumber = 0; // current candidate number
with the maximum number of points.
*/ static List<Point> GetBestCandidate(List<Point> points, double maxDiameter)
{
double maxDiameterSquared = maxDiameter * maxDiameter; // square maximum diameter List<List<Point>> candidates = new List<List<Point>>(); // stores all candidate clusters List<Point> currentCandidate = null; // stores current candidate cluster int[] candidateNumbers = new int[points.Count]; // keeps track of candidate numbers to which points have been allocated int totalPointsAllocated = 0; // total number of points already allocated to candidates int currentCandidateNumber = 0; // current candidate number
for(int i = 0; i < points.Count; i++)
{
if (totalPointsAllocated == points.Count) break; // no need to continue further if (candidateNumbers[i] > 0) continue; // point already allocated to a candidate currentCandidateNumber++;
currentCandidate = new List<Point>(); // create a new candidate cluster currentCandidate.Add(points[i]); // add the current point to it candidateNumbers[i] = currentCandidateNumber;
totalPointsAllocated++;
{
if (totalPointsAllocated == points.Count) break; // no need to continue further if (candidateNumbers[i] > 0) continue; // point already allocated to a candidate currentCandidateNumber++;
currentCandidate = new List<Point>(); // create a new candidate cluster currentCandidate.Add(points[i]); // add the current point to it candidateNumbers[i] = currentCandidateNumber;
totalPointsAllocated++;
Point latestPoint = points[i]; // latest point added to current cluster int[] diametersSquared = new int[points.Count]; // diameters squared of each point when added to current cluster
// iterate through any remaining points // successively selecting the point closest to the group until the threshold is exceeded while (true)
{
if (totalPointsAllocated == points.Count) break; // no need to continue further int closest = -1; // index of closest point to current candidate cluster int minDiameterSquared = Int32.MaxValue; // minimum diameter squared, initialized to impossible value
{
if (totalPointsAllocated == points.Count) break; // no need to continue further int closest = -1; // index of closest point to current candidate cluster int minDiameterSquared = Int32.MaxValue; // minimum diameter squared, initialized to impossible value
for (int j = i + 1; j < points.Count; j++)
{
if(candidateNumbers[j] > 0) continue; // point already allocated to a candidate
{
if(candidateNumbers[j] > 0) continue; // point already allocated to a candidate
// update diameters squared to allow for latest point added to current cluster int distSquared = Point.DistanceSquared(latestPoint, points[j]);
if (distSquared > diametersSquared[j]) diametersSquared[j] = distSquared;
if (distSquared > diametersSquared[j]) diametersSquared[j] = distSquared;
// check if closer than previous closest point if (diametersSquared[j] < minDiameterSquared)
{
minDiameterSquared = diametersSquared[j];
closest = j;
}
}
// if closest point is within maxDiameter, add it to the current candidate and mark it accordingly if ((double)minDiameterSquared <= maxDiameterSquared)
{
currentCandidate.Add(points[closest]);
candidateNumbers[closest] = currentCandidateNumber;
totalPointsAllocated++;
}
else // otherwise finished with current candidate {
break;
}
}
{
minDiameterSquared = diametersSquared[j];
closest = j;
}
}
// if closest point is within maxDiameter, add it to the current candidate and mark it accordingly if ((double)minDiameterSquared <= maxDiameterSquared)
{
currentCandidate.Add(points[closest]);
candidateNumbers[closest] = currentCandidateNumber;
totalPointsAllocated++;
}
else // otherwise finished with current candidate {
break;
}
}
// add current candidate to candidates list candidates.Add(currentCandidate);
}
}
// now find the candidate cluster with the largest number of points int maxPoints = -1; // impossibly small value int bestCandidateNumber = 0; // ditto for(int i = 0; i < candidates.Count; i++)
{
if (candidates[i].Count > maxPoints)
{
maxPoints = candidates[i].Count;
bestCandidateNumber = i + 1; // counting from 1 rather than 0 }
}
{
if (candidates[i].Count > maxPoints)
{
maxPoints = candidates[i].Count;
bestCandidateNumber = i + 1; // counting from 1 rather than 0 }
}
// iterating backwards to avoid indexing problems, remove points in best candidate from points list // this will automatically be persisted to caller as List<Point> is a reference type for(int i = candidateNumbers.Length - 1; i >= 0; i--)
{
if (candidateNumbers[i] == bestCandidateNumber) points.RemoveAt(i);
}
// return best candidate to caller return candidates[bestCandidateNumber - 1];
}
}
READ MORE>>
{
if (candidateNumbers[i] == bestCandidateNumber) points.RemoveAt(i);
}
// return best candidate to caller return candidates[bestCandidateNumber - 1];
}
}
READ MORE>>
File Transfer Program using C#.Net Windows Application
How to easily send files (including Audio, Video, doc or any type of file) from Client to Server. System.Net.Sockets;using System.IO;
Then write code for the button1 (Browse) and the button2 (Send) as in the following.
For example:
CLIENT PROGRAM:
using System;
Then write code for the button1 (Browse) and the button2 (Send) as in the following.
For example:
CLIENT PROGRAM:
using System;
Outlook with .NET 2.0
Outlook Object Model Overview
For accessing the outlook and its features you have to add reference of Microsoft Outlook 11.0 object library Version 9.2 (COM component) to your project.This COM component provides various objects through we can access the outlook.
Extension Methods in C#
Extension methods make it possible to write a method to a class that doesn't offer the method at first. You can also add a method to any class that implements a specific interface, so multiple class can make use of the same implementation.
How to Create Excel file in ASP.NET C#
You can create Excel file using
- OpenXML
- Microsoft.Office.Interop.Excel
How to Show PDF file in C#
We know that PDF is not Microsoft technology; it is created by Adobe system and widely used for document exchange, and based on post script. The .Net framework does not provide a library to easily handle PDF files in .Net.
Copy a table using SQL Server Management Objects
To modify, create or delete a database object, every class (which represents a database entity like Table, Index and so on) contains a Alter, Drop and Create method. Every object contains a property Parent, so a Database object will return a Server object when the property Parent is called.
C# DateTime
In .NET Framework, a DateTime structure represents dates and times. We use DateTime to work with dates, times, and both. The value of DateTime is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.
Restricting User To Login Multiple Times Using Same Credentials
Generally in many websites after you get logged in you will be redirected to home page. While this page is running if the user selects the new window and open the Login page again and if he provides the same credentials which was provided earlier then he will be automatically redirected to home page.
Speech Recognition(Voice) using C#
This Article explains you how to Convert Entered text to Speech Recognition(Voice) using C#. Before Starting to Write a code for converting a text intoSpeech Recognition(Voice). You have to add Reference (Download if not available) Interop.SpeechLib.
How to send mail automatically for every five minutes using C#?
This article explain how to send mail automatically using window service, Concepts are involved for sending mail automatically are SMTP Server for sending mails, Window service used to mail automatically, Event log is used to see whether the window service is working, below explain detail.
Writing an ActiveX control in C#
An ActiveX control is an object that supports a customizable programmatic interface. Using the methods, events and properties exposed by the control, web developers can automate their web pages to give the functionality which is equivalent to that of a desktop application.
Shut down, restart, log off and forced log off system using C#
Here is the discussion of
- How to Shut Down a machine
- How to Log Off a machine
- How to forced log off a machine
- How to restart a machine using c#
Upload an Image and an Audio file using C#
Image Upload:
In this case, use a FileUpload Control(Id:->FileUpload1), an ImageButton(ImageButton1) or you can use the Image, a Button Control(Id:->Updatebttn) to upload the Image, Button Control(Id:->Addbttn) to add the Image in database, an image(which we want to upload) , add a new folder (Image) in the WebForm.
Working with Tuple in C# 4.0
Tuple can store n - number of values in it. The type of each of those variables as generic parameters, and the object will create those values for you.
What is a Virtual Directory and how to create it?
What is a virtual directory ?
A virtual directory represents a web application and it points to a physical folder in your computer.
A web application is accessed using a virtual directory name instead of a physical folder name. For example, if you have a web application called "Shopcart" in your machine, you will have a virtual directory for this web application. You will access your web application using the URL httP://localhost/Shopcart. If your virtaul directory name is "Test", then your web application url will be "http://localhost/Test".
A web application is accessed using a virtual directory name instead of a physical folder name. For example, if you have a web application called "Shopcart" in your machine, you will have a virtual directory for this web application. You will access your web application using the URL httP://localhost/Shopcart. If your virtaul directory name is "Test", then your web application url will be "http://localhost/Test".
Static and Dynamic web pages
We can broadly classify web sites and web pages into two categories:
1. Static web pages
2. Dynamic web pages
1. Static web pages
2. Dynamic web pages
Subscribe to:
Posts (Atom)